package com.clqteam.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.clqteam.model.Employee;
import com.clqteam.service.removeZsService;
import com.clqteam.serviceimpl.removeZsServiceImpl;

public class removeZsController extends HttpServlet{
	removeZsService upd = new removeZsServiceImpl();
	
	   @Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		   doPost(req, resp);
	}
	   
	   
	   @Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		 //获取系统时间
       	 Date date=new Date();
       	 SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       	 String drTime=format.format(date);
       	 //获取参数
       	String zspIds=req.getParameter("zspId");
     		int zspId  = Integer.parseInt(zspIds);
     		String zspAmounts=req.getParameter("zspAmount");
     		int zspAmount  = Integer.parseInt(zspAmounts);
     		String zsIds=req.getParameter("zsId");
     		int zsId  = Integer.parseInt(zsIds);
     		int oudCount = upd.getProductCountByZspid(zspId);
     		int pCount = upd.getProductCountByZspid(zspId);
     		int a = pCount + oudCount;
     		String zspName = req.getParameter("zspName");
     		String zspCost = req.getParameter("zspCost");
     		 int s= upd.updateShByZspid(zspId, a);
     		 int ss = upd.removeZsByZspid(zsId);
         	 String mm = Integer.toString(s);
         	 PrintWriter pw = resp.getWriter();
         	 pw.write(mm);
			
     	 //写入日志
        	 HttpSession session = req.getSession();
        	 Employee emp = (Employee) session.getAttribute("employee");
        	 String eName = emp.geteName();
        	 int eId = emp.geteId();
        
        	 StringBuffer drInfos = new StringBuffer();
       		drInfos.append("用户");
       		drInfos.append(eName);
       		drInfos.append("把自损编号为");
       		drInfos.append(zsId);
       		drInfos.append("的自损订单删除了，原自损商品名称为");
       		drInfos.append(zspName);
       		drInfos.append("Id为");
       		drInfos.append(zspId);
       		drInfos.append("自损数量为");
       		drInfos.append(zspAmounts);
       		drInfos.append("原进价为");
       		drInfos.append(zspCost);
       		
       		String drInfo = drInfos.toString();
             upd.setdirInfo(drInfo, drTime, eId, eName);
       	 
             double d = Double.valueOf(zspCost);
             double oMoney = d*zspAmount;
             int oId = upd.getOidByPid(zspId);
       	      upd.updataOrder(oId, oMoney);
       	      upd.updataOrderInfo(oId, zspAmount);
	}

}
